package cn.itshare.junit.xmltest;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import org.jdom.Document;
import org.jdom.Element;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.share.bean.oa.org.Department;
import com.share.bean.oa.org.EmpAccount;
import com.share.bean.oa.org.EmpCard;
import com.share.bean.oa.org.Employee;
import com.share.bean.oa.org.Organization;
import com.share.bean.sysmgr.BackTreeMenu;
import com.share.bean.sysmgr.privilege.PrivilegeResource;
import com.share.bean.sysmgr.privilege.PrivilegeResourceItem;
import com.share.bean.sysmgr.privilege.PrivilegeRole;
import com.share.commons.enumeration.Gender;
import com.share.commons.util.LoadXmlUtil;
import com.share.commons.util.RandomUtil;
import com.share.commons.util.ValidateEmail;
import com.share.service.oa.org.DepartmentManager;
import com.share.service.oa.org.EmployeeManager;
import com.share.service.oa.org.OrganizationManager;
import com.share.service.sysmgr.BackTreeMenuManager;
import com.share.service.sysmgr.privilege.PrivilegeResourceItemManager;
import com.share.service.sysmgr.privilege.PrivilegeResourceManager;
import com.share.service.sysmgr.privilege.PrivilegeRoleManager;

/**
 * 说明：初始化超级管理员账号，以及权限
 * <br/>编写者：杨浩泉
 * <br/>日期：2010-3-1
 */
public class InitSystemAdministrator {

	private static final String PATH = "D:/Workspaces8.5/share/share/WebRoot/WEB-INF/tree/" ;
	
	@Resource
	private PrivilegeResourceItemManager resourceItem ;
	@Resource
	private PrivilegeResourceManager resource ;
	@Resource
	private PrivilegeRoleManager role ;
	@Resource
	private OrganizationManager org ;
	@Resource
	private DepartmentManager dept ;
	@Resource
	private EmployeeManager emp ;
	@Resource
	private BackTreeMenuManager back_index_menu ;
	
	public static void main(String[] args) throws Exception {
		InitSystemAdministrator init = new InitSystemAdministrator() ;
		init.initAction() ;
	}
	
	public void initAction() throws Exception {
		ClassPathXmlApplicationContext c = new ClassPathXmlApplicationContext("spring/bean*.xml") ;
		resourceItem = (PrivilegeResourceItemManager)c.getBean("privilegeResourceItemManager") ;
		resource = (PrivilegeResourceManager)c.getBean("privilegeResourceManager") ;
		role = (PrivilegeRoleManager)c.getBean("privilegeRoleManager") ;
		org = (OrganizationManager)c.getBean("organizationManagerImpl") ;
		dept = (DepartmentManager)c.getBean("departmentManagerImpl") ;
		emp = (EmployeeManager)c.getBean("employeeManagerImpl") ;
		back_index_menu = (BackTreeMenuManager) c.getBean("backTreeMenuManagerImpl") ;
		
		String[] index_menu = new String[10] ;
		
		List<Treevo> list = new ArrayList<Treevo>() ;
		for(int i=1; i<=10; i++) {
			Document doc = LoadXmlUtil.getDocument(PATH + "tree"+i+".xml");
			Element element = doc.getRootElement();
			
			index_menu[i-1] = ((Element)element.getChildren().iterator().next()).getAttributeValue("text") ;

			loadtree(element.getChildren(),list) ;
		}
		
		saveSuperAdministrator(list,index_menu) ;
	}
	
	/** 添加资源、创建角色、创建员工 
	 * @throws Exception */
	public void saveSuperAdministrator(List<Treevo> list,String[] index_menu) throws Exception {
		
		PrivilegeResourceItem pri = new PrivilegeResourceItem() ;
		pri.setSysItemName("系统所有资源") ;
		pri.setSysItemDesc("系统所有资源") ;
		
		Integer priID = (Integer) this.resourceItem.saveResourceItemManager(pri) ;
		
		Set<PrivilegeResource> res = new HashSet<PrivilegeResource>() ;
		for (Iterator<Treevo> iterator = list.iterator(); iterator.hasNext();) {
			Treevo treevo = (Treevo) iterator.next();
			
			PrivilegeResource r = new PrivilegeResource() ;
			r.setResId(priID) ;
			r.setResName(treevo.getName()) ;
			r.setResUrl(treevo.getUrl()) ;
			r.setResDesc(treevo.getName()) ;
			r.setResType("read") ;
			r.setResPriorty(1) ;
			this.resource.saveResourceManager(r) ;
			
			res.add(r) ;
		}
		
		PrivilegeRole r = new PrivilegeRole() ;
		r.setRoleName("超级管理员") ;
		r.setRoleDesc("拥有系统所有权限") ;
		r.setResource(res) ;
	
		role.saveRoleManager(r) ; //保存角色
		
		Object[] obj = orgdeptempVO() ;		
		
		org.saveOrgManager((Organization)obj[0]) ; //保存机构
		dept.saveDeptManager((Department)obj[1]) ; //保存部门
		
		Employee e = (Employee)obj[2] ;
		Set<PrivilegeRole> roleSet = new HashSet<PrivilegeRole>() ;
		roleSet.add(r) ;
		
		e.setRole(roleSet) ;
		
		emp.saveEmpManager(e) ; //保存员工
		
		for(int i=0; i<index_menu.length; i++) {
			BackTreeMenu btm = new BackTreeMenu() ;
			btm.setBtm_Name(index_menu[i]) ;
			back_index_menu.saveBackTreeMenuManager(btm) ;
		}
	}
	
	public Object[] orgdeptempVO() {
		Object[] obj = new Object[3] ;
		
		Organization o = new Organization() ;
		o.setOrgCode("tlgzs0001") ;
		o.setOrgLegalName("天狼工作室") ;
		o.setOrgLegalPeople("杨浩泉") ;
		o.setOrgZip("510210") ;
		o.setOrgAdminTel("020-38800000") ;
		o.setEstablishCountry("中国") ;
		o.setOrgAddr("广州市天河区壬丰大夏901") ;
		o.setOrgSynopsis("主要开发与J2EE B/S结构的业务系统，针对企业的业务进行系统开发") ;
		o.setOrgBak("无备注") ;
		o.setEstablishTime(new Date()) ;
		o.setAddTime(new Date()) ;
		
		Department d = new Department() ;
		d.setDeptNo("dev001310") ;
		d.setDeptName("软件工程部");
		d.setDeptContactPeople("杨浩泉") ;
		d.setDeptContactTel("020-38800001") ;
		d.setDeptMsg("管理其他软件开发部门") ;
		d.setDeptBak("无备注") ;
		d.setDeptCreTime(new Date()) ;
		
		d.setOrganization(o) ;

		Employee e = new Employee() ;
		e.setEmpNo("TL00133021") ;
		e.setEmpRealname("杨浩泉") ;
		e.setNation("汉族") ;
		e.setGender(Gender.MAN) ;
		e.setBirthday(new Date()) ;
		e.setTel("020-3353886") ;
		e.setAcceptDept("软件工程部") ;
		e.setAcceptJob("软件架构师") ;
		e.setSchool("华南理工大学") ;
		e.setEducational("专科") ;
		e.setSpeciality("计算机技术与科学") ;
		e.setAgeJob(4) ;
		e.setNatives("广东") ;
		e.setResidence("广州") ;
		e.setResidentialAddr("广州天河龙口西13号") ;
		e.setMobileTel("13580446087") ;
		e.setEmergency("杨浩泉") ;
		e.setEmergencyTel("13580446087") ;
		e.setEmail((ValidateEmail.checkMail("yanghaoquan217@gmail.com")==true?"yanghaoquan217@gmail.com":"")) ;
		e.setAppointmentDate(new Date()) ;
		e.setVisible(true) ;
		
		EmpAccount ea = new EmpAccount() ;
		ea.setAccount("yanghaoquan") ;
		ea.setPassword("yanghaoquan") ;

		EmpCard ec = new EmpCard() ;
		ec.setSocialNo(RandomUtil.getRandomNum(18)) ;
		ec.setBankNo(RandomUtil.getRandomNum(18)) ;
		ec.setCardNo(RandomUtil.getRandomNum(18)) ;
		
		e.setEmpAccount(ea) ;
		e.setEmpCard(ec) ;
		e.setDepartment(d) ;
		
		obj[0] = o ;
		obj[1] = d ;
		obj[2] = e ;
 		
		return obj ;
	}
	
	public static List<?> loadtree(List<?> element , List<Treevo> list) {
		for(int i=0; i<element.size(); i++) {
			Element el1 = (Element) element.get(i) ;
			if(el1.getName().equals("item")) {
				
				if(null != el1.getChildText("userdata") && !"".equals(el1.getChildText("userdata").trim())) {
					/*System.out.print(el1.getName()+"\t");
					System.out.print(el1.getAttributeValue("text")+"\t");
					System.out.println(el1.getChildText("userdata"));*/
					
					Treevo tvo = new Treevo() ;
					tvo.setName(el1.getAttributeValue("text")) ;
					tvo.setUrl(el1.getChildText("userdata")) ;
					
					list.add(tvo) ;
				}
			}
			
			if(el1.getChildren().size() > 0) {
				loadtree(el1.getChildren(),list) ;
			}
		}
		return list ;
	}
}

class Treevo {
	private String name ;
	private String url ;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getUrl() {
		return url;
	}
	public void setUrl(String url) {
		this.url = url;
	}
}

